package com.jn.listtest_2;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleAdapterActivity extends AppCompatActivity {
    private List<Map<String, Object>> mList;
    private ListView mListView;
    private int[] resArray = {R.mipmap.photo, R.mipmap.mine};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        mListView = findViewById(R.id.listview);
        mList = new ArrayList<>();
        initData();
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this,
                mList,
                R.layout.simple_adapter_item,
                new String[]{"photo", "name", "id", "age", "phone"},
                new int[]{R.id.photo, R.id.name, R.id.id, R.id.age, R.id.phone}){
            @Override
            public void setViewImage(ImageView v, int value) {
                v.setImageResource(value);
            }
        };

        mListView.setAdapter(simpleAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = mList.get(position);
                Log.e("test123", "===========" + map.get("name"));
            }
        });
    }

    private void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("photo", resArray[0]);
        map.put("name", "jim");
        map.put("id", "00000001");
        map.put("age", "20");
        map.put("phone", "13888888888");

        Map<String, Object> map1 = new HashMap<>();
        map1.put("photo", resArray[1]);
        map1.put("name", "tom");
        map1.put("id", "00000002");
        map1.put("age", "33");
        map1.put("phone", "13666666666");

        mList.add(map);
        mList.add(map1);
    }
}
